const { PrismaClient } = require('@prisma/client');
const bcrypt = require('bcrypt');

const prisma = new PrismaClient();

async function updateAdminPassword() {
  try {
    console.log('更新管理员密码...');
    
    // 查找管理员账号
    const admin = await prisma.adminUser.findFirst({
      where: { username: 'admin' }
    });
    
    if (!admin) {
      console.log('❌ 管理员账号不存在');
      return;
    }
    
    console.log('找到管理员账号，正在更新密码...');
    
    // 生成新密码哈希
    const newPassword = '123456';
    const hashedPassword = await bcrypt.hash(newPassword, 10);
    
    // 更新密码
    await prisma.adminUser.update({
      where: { id: admin.id },
      data: { password: hashedPassword }
    });
    
    console.log('✅ 密码更新成功');
    console.log(`   用户名: ${admin.username}`);
    console.log(`   新密码: ${newPassword}`);
    
    // 验证新密码
    const updatedAdmin = await prisma.adminUser.findFirst({
      where: { username: 'admin' }
    });
    
    const isValid = await bcrypt.compare(newPassword, updatedAdmin.password);
    if (isValid) {
      console.log('✅ 新密码验证成功');
    } else {
      console.log('❌ 新密码验证失败');
    }
    
  } catch (error) {
    console.error('❌ 错误:', error);
  } finally {
    await prisma.$disconnect();
  }
}

updateAdminPassword(); 